<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function isHappy(n) {
            let set = new Set();
            while (n !== 1 && !set.has(n)) {
                set.add(n);
                n = getNext(n);
            }
            return n === 1;
        }

        function getNext(n) {
            let sum = 0;
            while (n > 0) {
                sum += Math.pow(n % 10, 2);
                n = Math.floor(n / 10);
            }
            return sum;
        }
        console.log(isHappy(19));
    </script>
</body>
</html>